//
//  ITSTableViewCell.m
//  XCar
//
//  Created by Rainbow on 2/17/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "ITSTableViewCell.h"
#import "ITSGetImageForCellOperation.h"
#import "UtilHelper.h"
#import "UIHelper.h"

@implementation ITSTableViewCell
@synthesize stubImageName, imageCache, imageView, index, saved, shop,nameLabel,descrptionLabel;

- (id)initWithStyle:(UITableViewCellStyle)style
	reuseIdentifier:(NSString *)reuseIdentifier
	  stubImageName:(NSString *)stubImgName
		 imageCache:(NSMutableDictionary *)imgCache
               shop:(Shop *)aShop
              index:(NSInteger)aIndex
          imageRect:(CGRect)rect;
{
	if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
	{
        UIView *bgView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 80)] autorelease];
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		self.imageCache = imgCache;
		self.stubImageName = stubImgName;
        self.shop = aShop;
		imageView =[[UIImageView alloc] initWithFrame:rect];
        imageView.layer.cornerRadius = 8;
        imageView.layer.masksToBounds = YES;
        self.index = aIndex;
		[self addSubview:self.imageView];
		[self setupImageView];
        
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, 190, 20)];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.text = shop.name;
        [self addSubview:nameLabel];
        
        NSInteger descriptionHeight = [shop.description sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(180, 50) lineBreakMode:UILineBreakModeWordWrap].height;
        
        descrptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 30, 180, descriptionHeight)];
        descrptionLabel.backgroundColor = [UIColor clearColor];
        descrptionLabel.font = [UIFont systemFontOfSize:12];
        descrptionLabel.text = shop.description;
        descrptionLabel.numberOfLines = 0;
        [self addSubview:descrptionLabel];
        
		
	}
	return self;
}

- (void)updateWithShop:(Shop *)aShop
            imageCache:(NSMutableDictionary *)imgCache
                 index:(NSInteger)aIndex;
{
	self.imageCache = imgCache;
    self.index = aIndex;
    self.shop = aShop;
    self.nameLabel.text = shop.name;    
    NSInteger descriptionHeight = [shop.description sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(180, 50) lineBreakMode:UILineBreakModeWordWrap].height;
    self.descrptionLabel.frame = CGRectMake(100, 30, 180, descriptionHeight);
    self.descrptionLabel.text = shop.description;
	[self setupImageView];
}

- (void)imageDidLoad:(NSArray *)objs
{
    if (objs == nil) {
        [UIHelper hideProcessIndicatorWithView:self];
        return;
    }
    
    UIImage *img = [objs objectAtIndex:0];
	if(img == nil)
	{
		return;
	}
    if (self.index == [[objs objectAtIndex:1] intValue]) {
        if (self.imageCache != nil) {
            [self.imageCache setObject:img forKey:self.shop.shopId];
        }
        self.imageView.image = img;
    }
    
    [UIHelper hideProcessIndicatorWithView:self];
}

- (void)setupImageView
{	
    ImageInfo *imageInfo = nil;
    if ([shop.images count] > 0) {
        imageInfo = [shop.images objectAtIndex:0];
    }
    else
    {
        self.imageView.image = [UIImage imageNamed:self.stubImageName];
        return;
    }
    
	if ([self.imageCache objectForKey:self.shop.shopId] == nil) 
	{
        UIImage *image = [UtilHelper getImageFromDocument:imageInfo.thumbImagUrl];
        if (image) {
            
            [self.imageCache setObject:image forKey:self.shop.shopId];
            self.imageView.image = image;
        }
        else
        {
            UIImage *stubImage = [UIImage imageNamed:self.stubImageName];
            self.imageView.image = stubImage;
            if (!requestDataQueue)
            {
                requestDataQueue = [[NSOperationQueue alloc] init];
            }
            [requestDataQueue cancelAllOperations];
            
            ITSGetImageForCellOperation *operation = [[[ITSGetImageForCellOperation alloc] initWithTableCell:self imageUrl:[UtilHelper getFullImageUrl:imageInfo.thumbImagUrl] nameToSave:imageInfo.thumbImagUrl] autorelease];
            operation.index = self.index;
            [requestDataQueue addOperation:operation];
            
            [UIHelper hideProcessIndicatorWithView:self];
            
            [UIHelper showProcessIndicatorWithView:self.imageView atPoint:CGPointMake(self.imageView.frame.size.width/2, self.imageView.frame.size.height/2) hasMask:NO style:UIActivityIndicatorViewStyleGray];
        }
	}
	else 
	{
		self.imageView.image = [self.imageCache objectForKey:self.shop.shopId];
	}
}

- (void)setSelecting:(BOOL)canSelect
{
    if (canSelect) {
        self.accessoryType = UITableViewCellAccessoryNone;
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    else
    {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self setSelectionStyle:UITableViewCellSelectionStyleBlue];
    }
}

- (void)setSaved
{
    
}

-(void)dealloc
{
    self.imageView = nil;
    self.imageCache = nil;
    self.stubImageName = nil;
    self.shop = nil;
    self.nameLabel = nil;
    self.descrptionLabel = nil;
    if (requestDataQueue != nil) {
        [requestDataQueue cancelAllOperations];
        [requestDataQueue release];
    }
	[super dealloc];
}

@end
